<template>
  <div class="app-container">
    <el-row :gutter="20" class="mb8">
      <el-col :span="1.5">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-form-item label="选择模型">
                <el-select v-model="form.mid" placeholder="请选择模型">
                  <el-option
                    v-for="dict in midList"
                    :key="dict.value"
                    :label="dict.key"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-form-item label="测试数据" v-if="form.mid != null">
                <el-upload
                  class="upload-demo"
                  ref="upload"
                  action="#"
                  :on-preview="handlePreview"
                  :on-remove="handleRemove"
                  :before-remove="beforeRemove"
                  :http-request="uploadHttpRequest"
                  multiple
                  :limit="1"
                  :on-exceed="handleExceed"
                  :on-change="uploadFiles"
                  :file-list="fileList">
                  <el-button size="small" type="primary">点击上传</el-button>
                  <div slot="tip" class="el-upload__tip">只支持csv/mat格式文件</div>
                </el-upload>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm" v-if="form.mid != null">模型测试</el-button>
      <el-button @click="cancel" v-if="form.mid != null">取 消</el-button>
      <el-button type="success" icon="el-icon-download" @click="handleExport" v-if="form.mid != null">
        下载
      </el-button>
    </div>
    <br/>
    <el-table :data="testList" v-loading="loading">
      <el-table-column label="指标名称" align="center" prop="metrics"/>
      <el-table-column label="指标取值" align="center" prop="value"/>
      "
      <el-table-column label="测试结果" align="center" prop="result"/>
      <el-table-column label="指标描述" align="center" prop="description"/>
    </el-table>
  </div>
</template>

<script>
  import {listMangment} from "../../../api/bearing/mangment";
  import {getTest, modelTest} from "../../../api/bearing/test";

  export default {
    name: "Test",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 部门表格数据
        infoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          mid: null,
        },
        midList: [],
        fileList: [],
        testList: [],
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.$nextTick(() => {
        this.getList();
      })
    },
    methods: {
      /** 查询部门列表 */
      getList() {
        this.loading = true;
        listMangment(this.queryParams).then(response => {
          let a = response.rows;
          for (let i = 0; i < a.length; i++) {
            let b = {key: a[i].name, value: a[i].mid};
            this.midList.push(b);
          }
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        //this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          mid: null,
          files: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.fileList = null;
        this.$refs.upload.clearFiles();
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加部门";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids;
      },
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      },
      uploadHttpRequest(data) {
      },
      handleExceed(files, fileList) {
        this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
      },
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${file.name}？`);
      },
      uploadFiles(item, fileList) {
        this.form.files = fileList
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除部门编号为"' + ids + '"的数据项？').then(function () {
          return delInfo(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.loading = true;
            let formData = new FormData();
            if ("undefined" === typeof (this.form.files)) {
              this.$modal.msgError("请上传测试文件");
              return;
            }
            formData.append('files', this.form.files[0].raw);
            for (let key in this.form) {
              if (key !== "files") {
                formData.append(key, this.form[key])
              }
            }
            modelTest(formData).then(response => {
              let msg = response['msg'];
              if ("请检查文件格式" !== msg) {
                getTest(formData).then(response => {
                  this.testList = response.rows;
                })
              }
              this.total = response.total;
              this.loading = false;
            }).catch(() => {
              this.loading = false;
            });
          }
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.queryParams.mid = this.form.mid;
        this.download('model/test/export', {
          ...this.queryParams
        }, `test_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
